/**
 * Copyright 2015 iMeth工作室 (http://www.imeth.cn). All rights reserved.
 * Encoding UTF-8
 * Version: 1.0
 * Date: 2015年3月30日
 * History:	
 */

package cn.imeth.app;

import android.app.Activity;
import android.app.DialogFragment;
import android.os.Bundle;
import android.widget.Toast;

/**
 * @author 戴文龙 (daiwenlong@imeth.cn)
 * @version Revision: 1.0.0 Date: 2015年3月30日
 */
public abstract class ImethActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    private DialogFragment loadingDialog;

    protected void showAsyncProgressDialog() {
        if (loadingDialog == null) {
            loadingDialog = createLoadingDialog();
        }

        loadingDialog.show(getFragmentManager(), "LoadingDialog");

    }

    protected DialogFragment createLoadingDialog() {
        return new DialogFragment();
    }

    protected void hideProcessingIndicator() {
        if (loadingDialog != null) {
            loadingDialog.dismiss();
        }
    }

    private boolean isQuitPrompt = false;
    private long lastShowQuitPromptTime;
    private long quitPromptTime = 3000L;

    protected void makeToast(String msg) {
        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
    }

    protected void makeToast(int msgRes) {
        Toast.makeText(this, msgRes, Toast.LENGTH_SHORT).show();
    }

    /**
     * 退出Activity是否提示
     *
     * @param isQuitPrompt True 有提示 False 没有提示
     */
    protected void setQuitPrompt(boolean isQuitPrompt) {
        this.isQuitPrompt = isQuitPrompt;
    }

    protected String getQuitPrompt() {
        return "再按一次退出";
    }

    protected void setquitPromptTime(long quitPromptTime) {
        this.quitPromptTime = quitPromptTime;
    }

    @Override
    public void onBackPressed() {

        if (!isTaskRoot() || !this.isQuitPrompt) {
            super.onBackPressed();
            return;
        }

        if ((this.lastShowQuitPromptTime == 0L) || (System.currentTimeMillis() - this.lastShowQuitPromptTime > quitPromptTime)) {
            makeToast(getQuitPrompt());
            this.lastShowQuitPromptTime = System.currentTimeMillis();
            return;
        }

        this.lastShowQuitPromptTime = 0L;
        super.onBackPressed();
    }

}
